<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="headerTitle">
        <img src="../../assets/logo.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <!-- logout 退出 -->
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 中部 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 头部折叠菜单 -->
        <div class="asideToggle" @click="isCollapse=!isCollapse">
          <i :class="isCollapse?'el-icon-s-unfold':'el-icon-s-fold'"></i>
        </div>
        <el-menu
          v-for="item in userMenusList"
          :key="item.id"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#0394FA"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 一级导航 -->
          <el-submenu :index="item.path" collapse>
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              v-for="smallItem in item.children"
              :key="smallItem.id"
              :index="smallItem.path"
            >
              <i class="el-icon-menu"></i>
              {{ smallItem.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { Menus } from '@/api/api.js'
export default {
  name: 'BackShopHome',

  data () {
    return {
      userMenusList: [],
      // 图标
      iconList: {
        "125": "el-icon-user",
        "103": "el-icon-edit",
        "101": "el-icon-shopping-bag-1",
        "102": "el-icon-document-copy",
        "145": "el-icon-data-line",
      },
      // 折叠左侧菜单
      isCollapse: false
    };
  },
  created () {
    // 请求用户数据列表
    this.getMenus()
  },
  mounted () {

  },

  methods: {
    // 退出管理系统清除本地token
    logout () {
      window.sessionStorage.clear('token');
      // 跳转login页面
      this.$router.replace('/login');
      this.$message.success('退出成功')
    },
    async getMenus () {
      const { data: res } = await Menus()
      // const { data: res } = await this.$http.get('menus');
      // res.data.forEach((item, i) => {
      //   if (i === 2) {
      //     const arr =
      //     {
      //       authName: '添加商品',
      //       order: '1',
      //       path: 'add_Goods'
      //     }
      //       ;
      //     item.children.unshift(arr)
      //   }
      // })
      this.userMenusList = res.data;
    }
    // 请求用户数据列表

  },
};
</script>

<style lang="less" scoped>
// 设置主体颜色
// 头部
.el-header {
  background-color: #373d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // 头像
  .headerTitle {
    img {
      width: 50px;
      vertical-align: middle;
    }
    span {
      font-size: 16px;
      color: white;
      margin-left: 20px;
    }
  }
}
// 侧边栏
.el-aside {
  background-color: #313743;
  .el-menu {
    border-right: none;
  }
  // 头部折叠菜单
  .asideToggle {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    cursor: pointer;
    i {
      font-size: 18px;
      color: white;
    }
  }
}
.home-container {
  height: 100%;
}
.el-main {
  background-color: #f3f9ff;
}
</style>